package com.suredy.app.porject.ctrl.view;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.suredy.app.porject.roles.SystemAtomRoles;
import com.suredy.security.model.User;
import com.suredy.security.model.User2Role;

/**
 * 视图权限控制
 * 
 * @author VIVID.G
 * @since 2016-1-26
 * @version v0.1
 */
public class ViewRoleCtrl {

	protected Set<Character> roles = new HashSet<Character>();
	protected User user;

	public ViewRoleCtrl(List<User2Role> roles, User user) {
		this.user = user;
		this.recordRoles(roles);
	}

	public Integer getCompanyIndex() {
		if (this.isTianxingPeople())
			return 0;

		if (this.isZhongzhiPeople())
			return 1;

		return 9;
	}

	public boolean isSystemConfig() {
		return this.isHasRole(SystemAtomRoles.SYS_CONFIG);
	}

	public boolean isTianxingPeople() {
		return this.isHasRole(SystemAtomRoles.PEOPLE_FOR_TIANXING);
	}

	public boolean isZhongzhiPeople() {
		return this.isHasRole(SystemAtomRoles.PEOPLE_FOR_ZHONGZHI);
	}

	public boolean isOtherPeople() {
		return this.isHasRole(SystemAtomRoles.PEOPLE_FOR_OTHER);
	}

	public boolean isGongsiLeader() {
		return this.isHasRole(SystemAtomRoles.LEADER_GONGSI);
	}

	public boolean isShichangLeader() {
		return this.isHasRole(SystemAtomRoles.LEADER_SHICHANG);
	}

	public boolean isBidZhuanze() {
		return this.isHasRole(SystemAtomRoles.ZHUANZE_BID);
	}

	public boolean isContractZhuanze() {
		return this.isHasRole(SystemAtomRoles.ZHUANZE_CONTRACT);
	}

	public boolean isYewuManager() {
		return this.isHasRole(SystemAtomRoles.MANAGER_YEWU);
	}

	public boolean isKehuManager() {
		return this.isHasRole(SystemAtomRoles.MANAGER_KEHU);
	}

	public boolean isShengjiLeader() {
		return this.isHasRole(SystemAtomRoles.LEADER_SHENGJI);
	}

	public boolean isTongjiZhuanze() {
		return this.isHasRole(SystemAtomRoles.ZHUANZE_TONGJI);
	}

	public boolean isCaigouZhuanze() {
		return this.isHasRole(SystemAtomRoles.ZHUANZE_CAIGOU);
	}

	public boolean isMoneyInZhuanze() {
		return this.isHasRole(SystemAtomRoles.ZHUANZE_MONEY_IN);
	}

	public boolean isMoneyOutZhuanze() {
		return this.isHasRole(SystemAtomRoles.ZHUANZE_MONEY_OUT);
	}

	public boolean isXiangmuHeader() {
		return this.isHasRole(SystemAtomRoles.HEADER_XIANGMU);
	}

	public boolean isJijingHeader() {
		return this.isHasRole(SystemAtomRoles.HEADER_JIJING);
	}

	public boolean isCreateBid() {
		return this.isHasRole(SystemAtomRoles.BID_CREATE);
	}

	public boolean isCreateMainContract() {
		return this.isHasRole(SystemAtomRoles.CONTRACT_CREATE_OF_MAIN);
	}

	public boolean isCreateTaskContract() {
		return this.isHasRole(SystemAtomRoles.CONTRACT_CREATE_OF_TASK);
	}

	public boolean isCreateMaterialContract() {
		return this.isHasRole(SystemAtomRoles.CONTRACT_CREATE_OF_MATERIAL);
	}

	public boolean isCreateServiceContract() {
		return this.isHasRole(SystemAtomRoles.CONTRACT_CREATE_OF_SERVICE);
	}

	public boolean isCreateProfessionalContract() {
		return this.isHasRole(SystemAtomRoles.CONTRACT_CREATE_OF_PROFESSIONAL);
	}

	public boolean isCreateAgreementContract() {
		return this.isHasRole(SystemAtomRoles.CONTRACT_CREATE_OF_AGREEMENT);
	}

	public boolean isMoneyIn() {
		return this.isHasRole(SystemAtomRoles.MONEY_IN);
	}

	public boolean isMoneyOut() {
		return this.isHasRole(SystemAtomRoles.MONEY_OUT);
	}

	public boolean isMoneyInPlan() {
		return this.isHasRole(SystemAtomRoles.PLAN_MONEY_IN);
	}

	public boolean isMoneyOutPlan() {
		return this.isHasRole(SystemAtomRoles.PLAN_MONEY_OUT);
	}

	public boolean isWorkPlan() {
		return this.isHasRole(SystemAtomRoles.PLAN_WORK);
	}

	public boolean isStartContract() {
		return this.isHasRole(SystemAtomRoles.CONTRACT_START);
	}

	public boolean isClearContract() {
		return this.isHasRole(SystemAtomRoles.CONTRACT_CLEAR);
	}

	public boolean isShichangPeople() {
		if (this.isShichangLeader())
			return true;

		if (this.isBidZhuanze())
			return true;

		if (this.isContractZhuanze())
			return true;

		if (this.isYewuManager())
			return true;

		if (this.isKehuManager())
			return true;

		return false;
	}

	public boolean isShengjiPeople() {
		if (this.isShengjiLeader())
			return true;

		if (this.isTongjiZhuanze())
			return true;

		if (this.isCaigouZhuanze())
			return true;

		if (this.isMoneyInZhuanze())
			return true;

		if (this.isMoneyOutZhuanze())
			return true;

		if (this.isXiangmuHeader())
			return true;

		if (this.isJijingHeader())
			return true;

		return false;
	}

	protected boolean isHasRole(char role) {
		return this.roles.contains(role);
	}

	private void recordRoles(List<User2Role> urs) {
		if (urs == null || urs.isEmpty())
			return;

		for (User2Role ur : urs) {
			if (ur == null)
				continue;

			String code = ur.getRoleCode();

			if (StringUtils.isBlank(code))
				continue;

			this.addRoles(code.toCharArray());
		}
	}

	private void addRoles(char[] cs) {
		if (cs == null || cs.length <= 0)
			return;

		for (char c : cs) {
			this.roles.add(c);
		}
	}

}
